package com.cmict.data.infra.controller.admin.consultcategory;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.*;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.cmict.data.framework.common.pojo.PageResult;
import com.cmict.data.framework.common.pojo.CommonResult;
import static com.cmict.data.framework.common.pojo.CommonResult.success;

import com.cmict.data.framework.excel.core.util.ExcelUtils;

import com.cmict.data.framework.operatelog.core.annotations.OperateLog;
import static com.cmict.data.framework.operatelog.core.enums.OperateTypeEnum.*;

import com.cmict.data.infra.controller.admin.consultcategory.vo.*;
import com.cmict.data.infra.dal.dataobject.consultcategory.ConsultCategoryDO;
import com.cmict.data.infra.convert.consultcategory.ConsultCategoryConvert;
import com.cmict.data.infra.service.consultcategory.ConsultCategoryService;

@Api(tags = "管理后台 - 咨询分类")
@RestController
@RequestMapping("/infra/consult-category")
@Validated
public class ConsultCategoryController {

    @Resource
    private ConsultCategoryService consultCategoryService;

    @PostMapping("/create")
    @ApiOperation("创建问答分类")
    @PreAuthorize("@ss.hasPermission('sys:consult-category:create')")
    public CommonResult<Long> createConsultCategory(@Valid @RequestBody ConsultCategoryCreateReqVO createReqVO) {
        return success(consultCategoryService.createConsultCategory(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新问答分类")
    @PreAuthorize("@ss.hasPermission('sys:consult-category:update')")
    public CommonResult<Boolean> updateConsultCategory(@Valid @RequestBody ConsultCategoryUpdateReqVO updateReqVO) {
        consultCategoryService.updateConsultCategory(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除问答分类")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('sys:consult-category:delete')")
    public CommonResult<Boolean> deleteConsultCategory(@RequestParam("id") Long id) {
        consultCategoryService.deleteConsultCategory(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得问答分类")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('sys:consult-category:query')")
    public CommonResult<ConsultCategoryRespVO> getConsultCategory(@RequestParam("id") Long id) {
        ConsultCategoryDO consultCategory = consultCategoryService.getConsultCategory(id);
        return success(ConsultCategoryConvert.INSTANCE.convert(consultCategory));
    }

    @GetMapping("/list")
    @ApiOperation("获得问答分类列表")
    @PreAuthorize("@ss.hasPermission('sys:consult-category:query')")
    public CommonResult<List<ConsultCategoryRespVO>> getConsultCategoryList(ConsultCategoryBaseVO baseVO ) {
        List<ConsultCategoryDO> list = consultCategoryService.getConsultCategoryList(baseVO);
        return success(ConsultCategoryConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得问答分类分页")
    @PreAuthorize("@ss.hasPermission('sys:consult-category:query')")
    public CommonResult<PageResult<ConsultCategoryRespVO>> getConsultCategoryPage(@Valid ConsultCategoryPageReqVO pageVO) {
        PageResult<ConsultCategoryDO> pageResult = consultCategoryService.getConsultCategoryPage(pageVO);
        return success(ConsultCategoryConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出问答分类 Excel")
    @PreAuthorize("@ss.hasPermission('sys:consult-category:export')")
    @OperateLog(type = EXPORT)
    public void exportConsultCategoryExcel(@Valid ConsultCategoryExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<ConsultCategoryDO> list = consultCategoryService.getConsultCategoryList(exportReqVO);
        // 导出 Excel
        List<ConsultCategoryExcelVO> datas = ConsultCategoryConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "问答分类.xls", "数据", ConsultCategoryExcelVO.class, datas);
    }

}
